Skip to content

Retire the C++ MessageChannel impl - #8458

Merged
jasnell merged 2 commits into
mainfrom
jasnell/retire-ts-message-channel
Nov 19, 2026
Merged

jasnell merged 2 commits into
mainfrom
jasnell/retire-ts-message-channel

Conversation

@jasnell

@jasnell jasnell commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

The TS MessageChannel impl autogate is rolling out now. Remove the C++ impl

Will remain draft until the autogate is fully out.

It's out.

@jasnell
jasnell force-pushed the jasnell/retire-ts-message-channel branch from cd23e8f to b6fde4a Compare November 14, 2026 00:41
@jasnell
jasnell marked this pull request as ready for review November 16, 2026 16:36
@jasnell
jasnell requested review from a team as code owners November 16, 2026 16:36
@jasnell
jasnell force-pushed the jasnell/retire-ts-message-channel branch from b6fde4a to 11e1fdc Compare November 16, 2026 16:37
clanky-the-clanker[bot]

This comment was marked as resolved.

Comment thread types/defines/messagechannel.d.ts Resolved
@jasnell
jasnell force-pushed the jasnell/retire-ts-message-channel branch from 11e1fdc to 7b0ba95 Compare November 16, 2026 19:58
clanky-the-clanker[bot]

This comment was marked as resolved.

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving from a typings perspective.

@nathanwhit nathanwhit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

jasnell and others added 2 commits November 19, 2026 21:15
The TypeScript MessageChannel and MessagePort
(src/per_isolate/messagechannel/) are rolling out to production
under the TS_MESSAGE_CHANNEL autogate. This removes the gate and the
C++ classes in api/messagechannel.{h,c++}, leaving the TypeScript
classes as the only implementation. Configs that still name the gate
are unaffected: unknown autogate names are ignored.

Every Worker now gets the classes the gate gave, with the
differences from the C++ ones that the suite's divergence ledger
recorded: MessagePort's constructor inherits from EventTarget's,
both constructors' prototype is non-writable, calling either without
`new` throws the language's TypeError, `constructor` is the first of
MessagePort.prototype's own keys, and postMessage() recognizes a
proxy for an array as an array. These were accepted for the gate's
rollout.

- main.ts loads the module in every context, as it did with the gate
  on, and under expose_global_message_channel defines the globals
  where JSG_NESTED_TYPE defined the C++ classes.
- api/messagechannel.h keeps only MessageChannelModule, which hands
  the bootstrap's classes to node:worker_threads through
  cloudflare-internal:messagechannel.
- messageevent::MessagePort, the type of a MessageEvent's source and
  ports, holds any object branded workerd:MessagePort, no longer
  either that or a C++ port.
- With no C++ class to describe them, the TypeScript declarations of
  both classes move to types/defines/messagechannel.d.ts, and
  MessageEvent and ServiceWorkerGlobalScope name them through
  overrides. The generated declarations are unchanged but for their
  order. types/test/types/messagechannel.ts covers them.
- src/tests/events/messagechannel/ collapses to one set of cells:
  which-impl.js becomes flags.js, each branch keeps its TypeScript
  side, and the divergence ledger folds into the suite's
  specification. src/tests/events/AGENTS.md describes what retiring
  a gate does to a suite.
- Comments that compared the TypeScript implementation with the C++
  one now describe it on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Few Workers use MessageChannel or MessagePort, yet the per-isolate
bootstrap loaded their module in every context, so every Worker paid
for it at startup. The module now loads on first use:

- Under expose_global_message_channel, main.ts defines the globals as
  V8 lazy data properties, through a new
  utils.defineLazyDataProperty(target, key, specifier, enumerable),
  whose value is the key export of the bootstrap module specifier.
  Reading one loads the module, and V8 replaces the property with an
  ordinary data property holding the class. Unlike an accessor, the
  property is a data property throughout, with the attributes the
  suite pins, and assigning the global before reading it shadows it,
  as for any other global, without loading anything.
- cloudflare-internal:messagechannel loads it through
  requireBootstrapExport(), new in per-isolate-bootstrap.h, which
  returns a module's exports, requiring the module if it is not
  loaded yet.

Whichever comes first loads the module, and the other finds it in
the require() cache. A property still lazy when a zygote takes its
startup snapshot keeps the getter in the heap, so it is registered
as an external reference. Its data is the module's specifier, a
string, rather than a loader function: V8 serializes an accessor's
data into the isolate snapshot, which cannot hold a function of the
context. With a function there, a debug V8 refuses to build the
snapshot, and in a release build the restored Worker's first read
fails, finding no per-isolate bootstrap state.

The module's top level now runs after user code, so it must not
read what user code can change. `class MessagePort extends
EventTarget` read EventTarget.prototype: replacing that before first
use would have given ports the wrong prototype, or made the load
throw. MessagePort now extends a stand-in whose prototype is the one
primordials captured, and then inherits from EventTarget itself.

messagechannel-bootstrap-test pins when the module loads, which
JavaScript cannot observe, and the replaced EventTarget.prototype.
startup-snapshot-globals-test starts Workers from a snapshot whose
top level read the globals or left them lazy, and checks that they
get the classes node:worker_threads hands out, and can use them.
src/per_isolate/AGENTS.md recommends the pattern for other globals
few Workers need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasnell
jasnell force-pushed the jasnell/retire-ts-message-channel branch from 7b0ba95 to 08392de Compare November 19, 2026 21:17

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No findings: the C++ MessageChannel and its autogate are cleanly removed, the TypeScript module now loads on first use with tests that cover it, and the earlier AGENTS.md wording note is fixed.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.ts
  • types/generated-snapshot/index.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. Only the internal bootstrap can reach the lazy-property helper, and its module specifier comes from main.ts, not from user code.


📖 AGENTS.md: ✅ LGTM

The updated src/per_isolate/AGENTS.md, src/tests/events/AGENTS.md and src/tests/events/messagechannel/AGENTS.md match the single-implementation setup and the lazy-loading mechanism.


📋 Code Quality: ✅ LGTM

Nothing still refers to the removed C++ classes or the autogate. The lazy data property is safe to keep in a startup snapshot and leaves the property lazy when a read throws. A late module load uses the EventTarget captured at bootstrap, so user changes to EventTarget.prototype don't affect it. The type defines match the declarations they replace.


⚡ Performance: ✅ LGTM

No regressions. Loading the module on first use makes startup cheaper for Workers that never use MessageChannel.


📝 Documentation: ✅ LGTM

src/tests/events/AGENTS.md now separates suites that cover both implementations (eventsource/) from those that run against the TypeScript implementation alone (messagechannel/). docs/startup-snapshot.md matches the change.


Reviewed commit: 08392de9

@jasnell
jasnell merged commit fa96ea3 into main Nov 19, 2026
35 of 40 checks passed
@jasnell
jasnell deleted the jasnell/retire-ts-message-channel branch November 19, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants